# -*- coding: utf-8 -*-
from django.db import models
from django.utils import timezone
from sorl.thumbnail.fields import ImageField


class Consulta(models.Model):
    nom = models.CharField("Títol de la consulta",
                           max_length=255)
    descripcio_extesa = models.TextField("Descripció de la consulta)",
                                         max_length=255)
    logo_consulta = ImageField(upload_to='logos_programes/%s/',
                               blank=True)
    datafi = models.DateTimeField("Data de fi")

    def __unicode__(self):
        return unicode(self.nom)


class NfcTag(models.Model):
    tagid = models.CharField(max_length=255)  # Hex
    lat = models.FloatField()
    lon = models.FloatField()
    consulta = models.ForeignKey(Consulta)

    def __unicode__(self):
        return unicode(self.tagid)


class Dispositiu(models.Model):
    dispositiuid = models.IntegerField()

    def __unicode__(self):
        return unicode(self.dispositiuid)


class Vot(models.Model):
    vot = models.IntegerField()  # pot ser 0, 1 (si, no) o del 1 al 10
    dispositiu = models.ForeignKey(Dispositiu)
    consulta = models.ForeignKey(Consulta)
    nfctag = models.ForeignKey(NfcTag)
    utcdatetime = models.DateTimeField("Data del vot",
                                       default=timezone.now())

    def bool_vot(self):
        if self.vot == 1:
            return "Si"
        else:
            return "No"
    bool_vot.short_description = "Vot"
    bool_vot.allow_tags = False

    class Meta:
        unique_together = (("dispositiu", "consulta"),)

    def __unicode__(self):
        return unicode(self.vot)
